package com.example.carconsumer.controller.dl.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.carinterface.service.dl.LoginService;
import com.example.carinterface.service.dl.admin.CouponService;
import com.example.carinterface.utils.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "admin-优惠券积分")
@RestController
@RequestMapping("/admin/coupon")
@CrossOrigin
public class CouponController {

    @Reference(check = false)
    private CouponService couponService;
    @Reference(check = false)
    private LoginService loginService;

    /**
     * 查询所有优惠券
     *
     * @return
     */
    @PostMapping("/findCoupon")
    @ApiOperation("优惠券列表")
    public ResultJson<List<Map<String, Object>>> find(@ApiParam("receiveMethod:1兑换码，2完成活动后客户端领取（可不传）")
                                                          @RequestBody(required = false) Map<String,Object> map) {

        return couponService.find(map);

    }

    @PostMapping("/addCoupon")
    @ApiOperation("添加优惠券")
    public ResultJson<String> add(@ApiParam("couponName：优惠券名称，" +
            "grantMethod：发放方式：1系统发放，2活动发放，" +
            "receiveMethod：领取方式：1兑换码，2完成活动后客户领取，" +
            "couponRange：优惠券使用范围：1通用，2代办订单，3修车订单，4保险订单，" +
            "createTime：创建时间，" +
            "deadTime：结束时间，" +
            "couponNum：数量" +
            "couponPrice：抵消金额" +
            "isSuperposition：是否可叠加：-1否，1是") @RequestBody Map<String, Object> map) {
        return couponService.add(map);
    }

    @PostMapping("/updateCoupon")
    @ApiOperation("修改优惠券")
    public ResultJson<String> updateCoupon(@ApiParam("id:优惠券id，" +
            "couponName：优惠券名称，" +
            "grantMethod：发放方式：1系统发放，2活动发放，" +
            "receiveMethod：领取方式：1兑换码，2完成活动后客户领取，" +
            "couponRange：优惠券使用范围：1通用，2代办订单，3修车订单，4保险订单，" +
            "createTime：创建时间，" +
            "deadTime：结束时间，" +
            "couponNum：数量" +
            "couponPrice：抵消金额" +
            "isSuperposition：是否可叠加：-1否，1是") @RequestBody Map<String, Object> map) {
        return couponService.updateCoupon(map);
    }
    @PostMapping("/delCoupon/{id}")
    @ApiOperation("删除优惠券")
    public ResultJson<String> delCoupon(@PathVariable Integer id){
        return couponService.delCoupon(id);

    }


    @PostMapping("/findExchangeCode")
    @ApiOperation("查询兑换码")
    public ResultJson<List<Map<String, Object>>> findExchangeCode() {
        return couponService.findExchangeCode();
    }

    @PostMapping("/addExchangeCode")
    @ApiOperation("添加兑换码")
    public ResultJson<String> addexchangecode(@ApiParam("code：兑换码，" +
            "deadTime：开始时间，" +
            "createTime：结束时间，" +
            "exchangeNum：兑换数量，" +
            "coupon_id：关联的优惠券id，" +
            "") @RequestBody Map<String, Object> map) {
        return couponService.addExchangeCode(map);
    }

    @PostMapping("/updateExchangeCode")
    @ApiOperation("修改兑换码")
    public ResultJson<String> updateExchangeCode(@ApiParam("id:兑换码id，" +
            "code：兑换码，" +
            "deadTime：结束时间，" +
            "createTime：开始时间，" +
            "exchangeNum：兑换数量，" +
            "coupon_id：关联的优惠券id，" +
            "state：兑换码状态，1未使用，-1已使用") @RequestBody Map<String, Object> map) {
        return couponService.updateExchangeCode(map);
    }
    @PostMapping("/delExchangeCode/{id}")
    @ApiOperation("删除兑换码")
    public ResultJson<String> delExchangeCode(@PathVariable("id") Integer id){
        return couponService.delExchangeCode(id);
    }


    //检查验证码
    @ApiOperation("检测手机号")
    @PostMapping("/testTel/{tel}")
    public ResultJson<String> testTel(@PathVariable("tel") String tel){
        return couponService.testTel(tel);
    }
    @ApiOperation("发送兑换码")
    @PostMapping("/sendExchangeCode")
    public ResultJson<String> sendExchangeCode(@ApiParam("tel:手机号，\n" +
            "code：兑换码\n"
            ) @RequestBody Map<String ,String> map){
        ResultJson<String> resultJson = new ResultJson<>();
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("code", map.get("code"));
        boolean tel = loginService.senSms(map.get("tel"), LoginService.templateCode_exchange, map1);

        if (tel){
            resultJson.setSuccess("发送成功");
        }else {
            resultJson.setSuccess("发送失败");
        }
        return resultJson;
    }


    //修改是否可用
    @ApiOperation("修改是否可用")
    @PostMapping("/updateState")
    public ResultJson<String> updateState(@ApiParam("id:id," +
            "state:-1不可用（默认，可不传），1可用") @RequestBody Map<String,Object> map){
        return couponService.updateState(map);
    }

    //积分


}
